package com.kye.express.business.networkpoint_query;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.kye.addrpicker.AddressPickerFragment;
import com.kye.addrpicker.PickerListener;
import com.kye.addrpicker.model.AddressPickerResult;
import com.kye.addrpicker.model.City;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.networkpoint_query.ResultQueryWorkNet;

import java.util.ArrayList;

/**
 * 网点查询
 */
public class NetworkPointQueryActivity extends BaseToolbarActivity implements PickerListener, NetworkPointQueryActivityContract.NetworkPointQueryView {
    public static final String PARAM_PARENT_FIRST = "-1";//用来判断是否是第一次添加

    public static enum Levels {
        province, city, area, street
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, NetworkPointQueryActivity.class));
    }

    private Levels mCurrentNeedPickerLevel = Levels.street;//需要选择到哪个级别

    private AddressPickerResult mResult;

    private NetworkPointQueryActivityContract.NetworkPointQueryViewPresenter mPresenter;
    private Dialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBindingUtil.setContentView(this, R.layout.activity_networkpoint_query);
        setUpToolbar((Toolbar) findViewById(R.id.toolbar));
        mPresenter = new NetworkPointQueryActivityContract.NetworkPointQueryViewPresenter(this);
        mPresenter.bind(this);
        mResult = new AddressPickerResult();
        City city = new City();
        city.setParentID(PARAM_PARENT_FIRST);
        addFragment(city);

        setTitle(getString(R.string.common_network_point_query));
    }

    @Override
    protected void onDestroy() {
        mPresenter.unbind();
        super.onDestroy();
    }

    private void addFragment(City pickedCity) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        AddressPickerFragment fragment = AddressPickerFragment.newFromNetworkPointQueryInstances(pickedCity);
        ft.replace(R.id.fgcontains, fragment);
        ft.addToBackStack(null);//不需要返回上一层把这行去掉
        ft.commit();
    }

    /**
     * 返回地址选择结果
     */
    private void backWithData() {
        Logger.d("select address:" + mResult.getProvince() + mResult.getCity() + mResult.getArea() + mResult.getStreet());
        if (mResult != null) {
            mPresenter.loadDatas(mResult);
        }
    }

    @Override
    public void onPicked(City pickedCity) {
        if (mResult != null) {
            switch (pickedCity.getLevelType()) {
                case "1":
                    mResult.setProvince(pickedCity.getFullName());
                    break;
                case "2":
                    mResult.setCity(pickedCity.getFullName());
                    break;
                case "3":
                    mResult.setArea(pickedCity.getFullName());
                    break;
                case "4":
                    mResult.setStreet(pickedCity.getFullName());
                    break;
            }
            int levelType = 1;
            try {
                levelType = Integer.valueOf(pickedCity.getLevelType());
            } catch (Exception e) {

            }
            if (levelType >= (mCurrentNeedPickerLevel.ordinal() + 1)) {
                backWithData();
            } else {
                addFragment(pickedCity);
            }
        }
    }

    /**
     * 后退的时候按栈回退
     */
    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStackImmediate();
        } else {
            finish();
        }
    }

    public static boolean isLollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onComplete() {
        KyeProgressDialog.dismiss(mDialog);
    }

    @Override
    public void onLoading() {
        mDialog = KyeProgressDialog.show(this, mDialog);
    }

    @Override
    public void onLoaded(ArrayList<ResultQueryWorkNet> datas) {
        if (datas != null && datas.size() > 0) {
            NetworkPointMapActivity.startActivity(this,datas);
        }else{
            ToastManager.show(this, R.string.networkpoint_query_no_datas, Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg, Toast.LENGTH_SHORT);
    }
}
